﻿Ext.Loader.setPath('Happy', '../../src');
Ext.require('Happy.Ajax');

Ext.define('TestModel', {
    extend: 'Ext.data.Model',

    idProperty: 'Id',
    fields: ['Id', 'Name', 'Email'],
    proxy: {
        type: 'ajax',
        actionMethods: {
            create: 'GET',
            read: 'GET',
            update: 'GET',
            destroy: 'GET'
        },
        api: {
            create: 'server/create.txt',
            read: 'server/read.txt',
            load: 'server/load.txt',
            update: 'server/update.txt',
            destroy: 'server/destroy.txt'
        },
        reader: {
            type: 'json',
            root: 'items',
            idProperty: 'Id',
            messageProperty: 'message'
        },
        writer: {
            type: 'json',
            encode: true,
            root: 'item'
        }
    }
});

Ext.onReady(function () {
    Ext.create('Ext.container.Viewport', {
        layout: 'fit',
        items: {
            xtype: 'panel',
            title: 'Ajax 演示',
            tbar: [
                {
                    text: '加载记录', handler: function () {
                        Happy.Ajax.load(TestModel, '98BFE693-58F7-43E7-82A9-A77FEA41ED90', {
                            success: function (record, operation) {
                                Ext.Msg.show({
                                    title: record.get('Name'),
                                    msg: operation.response.responseText
                                });
                            }
                        });
                    }
                },
                {
                    text: '修改记录', handler: function () {
                        Happy.Ajax.load(TestModel, '98BFE693-58F7-43E7-82A9-A77FEA41ED90', {
                            success: function (record_i) {
                                Happy.Ajax.save(record_i, {
                                    success: function (record_j, operation) {
                                        Ext.Msg.show({
                                            title: record_j.get('Name'),
                                            msg: operation.response.responseText
                                        });
                                    }
                                });
                            }
                        });
                    }
                },
                {
                    text: '添加记录', handler: function () {
                        var record_i = TestModel.create({ Name: 'xxx', Email: 'xxx' });
                        Happy.Ajax.save(record_i, {
                            success: function (record_j, operation) {
                                Ext.Msg.show({
                                    title: record_j.get('Name'),
                                    msg: operation.response.responseText
                                });
                            }
                        });
                    }
                },
                {
                    text: '删除记录', handler: function () {
                        Happy.Ajax.load(TestModel, '98BFE693-58F7-43E7-82A9-A77FEA41ED90', {
                            success: function (record_i) {
                                Happy.Ajax.destroy(record_i, {
                                    success: function (record_j, operation) {
                                        Ext.Msg.show({
                                            title: record_j.get('Name'),
                                            msg: operation.response.responseText
                                        });
                                    }
                                });
                            }
                        });
                    }
                },
                {
                    text: '自定义通信', handler: function () {
                        Happy.Ajax.callAction({
                            url: 'server/action.txt',
                            success: function (result) {
                                Ext.Msg.show({
                                    title: '响应内容',
                                    msg: Ext.encode(result)
                                });
                            }
                        });
                    }
                }
            ]
        }
    });
});